/**
 * @author banglong on 2017/2/4.
 */
/**
 * @author banglong
 * @date 2016/8/23.
 */
define(['socketIO', 'Layer'], function (io, layer) {

  var socket = io.connect(location.protocol + '//' + location.hostname + ':9091');

  // 保存过所有 接收过的消息 做过滤
  var msgArray = JSON.parse(localStorage.getItem('msgArr') || '[]');
  localStorage.removeItem('msgArr');

  socket.on('connect', function () {
    console.log('Client has connected to the server!');
  });

  socket.on('disconnect', function () {
    console.log('The client has disconnected!');
  });

  // 显示服务器推送的消息
  socket.on('messageevent', function (data) {
    // 判断此条信息是否显示过
    if (data && data.msgContent && msgArray.indexOf(data.msgContent) === -1) {
      setTimeout(function () {
        var layerIndex = layer.confirm(data.msgContent, {
          area: 'auto',
          btn: ['确定'] //按钮
        }, function () {
          layer.close(layerIndex)
        });
        msgArray.push(data.msgContent);
      }, 1000);
    }
  });

  // 发送信息
  // setTimeout(function () {
  //     var jsonObject = {
  //         startDate: "2017-01-10",
  //         endDate: "2017-01-10",
  //         msgType: 'chat',
  //         msgContent: "非常好啊"
  //     };
  //     socket.emit('messageevent', jsonObject);
  // }, 1000);

  return socket;
});